<html>
<head><meta charset="utf-8"><title>issue-45696-dangly-paths · t-compiler/wg-nll · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122657-t-compiler/wg-nll/index.html">t-compiler/wg-nll</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths.html">issue-45696-dangly-paths</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="130282061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths/near/130282061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths.html#130282061">(Jul 25 2018 at 15:27)</a>:</h4>
<p>Just to spite <span class="user-mention" data-user-id="116155">@Jake Goulding</span>, I'm going to use the "dangly paths" jargon here -- but this thread is devoted to figuring out how to resolve <a href="https://github.com/rust-lang/rust/issues/45696" target="_blank" title="https://github.com/rust-lang/rust/issues/45696">#45696</a>. There is some background discussion on the general problem in <a href="https://github.com/nikomatsakis/nll-rfc/issues/40" target="_blank" title="https://github.com/nikomatsakis/nll-rfc/issues/40">https://github.com/nikomatsakis/nll-rfc/issues/40</a> as well.</p>
<p>In any case, I think <span class="user-mention" data-user-id="116083">@pnkfelix</span> that we probably just want to hardcode a rule around <code>Box</code> for the time-being...</p>



<a name="130282087"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths/near/130282087" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths.html#130282087">(Jul 25 2018 at 15:27)</a>:</h4>
<p>specifically, some kind of rule saying that when a <code>Box </code> is dropped, it's ok to have borrows of its referent</p>



<a name="130282196"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths/near/130282196" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths.html#130282196">(Jul 25 2018 at 15:29)</a>:</h4>
<p>the example from <a href="https://github.com/rust-lang/rust/issues/45696" target="_blank" title="https://github.com/rust-lang/rust/issues/45696">#45696</a>:</p>
<div class="codehilite"><pre><span></span><span class="cp">#![feature(nll)]</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="nb">Box</span><span class="o">&lt;&amp;</span><span class="k">mut</span><span class="w"> </span><span class="kt">i32</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">*</span><span class="n">x</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</pre></div>


<p>the problem here is that we drop <code>x</code> on exit from the fn -- even though <code>**x</code> is still borrowed (note that the return is actually implicitly equal to <code>&amp;mut **x</code>, even though it looks like a move)</p>



<a name="130304397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths/near/130304397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths.html#130304397">(Jul 25 2018 at 21:49)</a>:</h4>
<p>I had a look at special casing Box recently.</p>



<a name="130304579"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths/near/130304579" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths.html#130304579">(Jul 25 2018 at 21:52)</a>:</h4>
<p>Some notes: we currently drop individual fields of <code>!Drop</code> structs/tuples/closures/generators and only access the fields that need drop (so <code>&amp;mut T</code> fields don't have an access allowing their referent to be borrowed.</p>



<a name="130304596"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths/near/130304596" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths.html#130304596">(Jul 25 2018 at 21:53)</a>:</h4>
<p>Storage dead stops the actual reference being borrowed)</p>



<a name="130304750"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths/near/130304750" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths.html#130304750">(Jul 25 2018 at 21:56)</a>:</h4>
<p>However, this can't be naively extended to Box, by making Drop do a deep access of the field of <code>*x</code> that implement <code>Drop</code>, followed by a shallow access of <code>*x</code>, since this causes dropping <code>struct A(Box&lt;A&gt;)</code> to give an infinite loop.</p>



<a name="130304850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths/near/130304850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths.html#130304850">(Jul 25 2018 at 21:58)</a>:</h4>
<p>So it seems like the special casing would have to be when/after checking for conflicts, which probably saves some time checking whether types implement <code>Drop</code> when it doesn't matter.</p>



<a name="130335204"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths/near/130335204" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/122657-t-compiler/wg-nll/topic/issue-45696-dangly-paths.html#130335204">(Jul 26 2018 at 10:43)</a>:</h4>
<blockquote>
<p>specifically, some kind of rule saying that when a <code>Box </code> is dropped, it's ok to have borrows of its referent</p>
</blockquote>
<p>yeah okay, I'll try to do this</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>